#include <QStringList>
#include <QMessageBox>

#include "qnigaapp.h"
#include "ui/factory.h"
#include "config/loader.h"
#include "config/cmdhandler.h"

QnigaApp* QnigaApp::ms_App;

QnigaApp::QnigaApp (int& _argc, char** _argv)
        : QApplication(_argc, _argv)
{
    ms_App = this;

    ProcessCfgFile();

    ProcessCmdLine();

    // Create UI
    m_UI.reset(QnigaUI::Factory::CreateUI(GetCoreConfig().GetUIType()));
}

void QnigaApp::ProcessCfgFile()
{
    Config::Loader cfgLoader(m_CfgManager);
}

void QnigaApp::ProcessCmdLine()
{
    try
    {
        Config::CmdHandler cmdHandler(m_CfgManager, arguments());
    }
    catch (const Config::UnknownCmdSwitchEx& _ex)
    {
        ReportErrorUnknownSwitch(_ex.what());
    }
}

// TODO: move to UI
void QnigaApp::ReportErrorUnknownSwitch (const QString& _switch) const
{
    QMessageBox::critical(NULL, tr("Command line error"),
                          tr("Unknown switch: \"%1\"").arg(_switch));
}

QnigaApp& QnigaApp::Get ()
{
    return *ms_App;
}

QnigaApp::~QnigaApp ()
{
}

QnigaUI::I& QnigaApp::GetUI ()
{
    return *m_UI.get();
}

const Config::Core::I& QnigaApp::GetCoreConfig () const
{
    return m_CfgManager;
}

const Config::GUI::I& QnigaApp::GetGUIConfig () const
{
    return m_CfgManager;
}

#include "qnigaapp.moc"
